package com.smart.community.commons.controller;

import com.smart.community.commons.aspect.FeignCallMonitorAspect;
import com.smart.community.commons.service.ServiceHealthMonitor;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

/**
 * 监控数据控制器
 * 
 * 提供监控数据的API接口
 * 包括服务调用统计、健康状态等信息
 * 
 * @author Wu.Liang
 * @version 2.0.0
 * @since 2025-08-28
 */
@Tag(name = "监控数据控制器", description = "提供监控数据的API接口")
@RestController
@RequestMapping("/api/monitor")
@Slf4j
public class MonitorController {
    
    @Autowired
    private ServiceHealthMonitor serviceHealthMonitor;
    
    /**
     * 获取服务调用统计信息
     * 
     * @return 调用统计信息
     */
    @Operation(summary = "获取服务调用统计信息")
    @GetMapping("/call-statistics")
    public Map<String, Object> getCallStatistics() {
        log.info("获取服务调用统计信息");
        
        Map<String, Object> result = new HashMap<>();
        result.put("success", true);
        result.put("data", FeignCallMonitorAspect.getAllCallStatistics());
        result.put("timestamp", System.currentTimeMillis());
        
        return result;
    }
    
    /**
     * 获取指定服务的调用统计信息
     * 
     * @param serviceKey 服务键名
     * @return 调用统计信息
     */
    @Operation(summary = "获取指定服务的调用统计信息")
    @GetMapping("/call-statistics/{serviceKey}")
    public Map<String, Object> getCallStatisticsByService(@PathVariable String serviceKey) {
        log.info("获取服务调用统计信息，服务: {}", serviceKey);
        
        Map<String, Object> result = new HashMap<>();
        FeignCallMonitorAspect.CallStatistics statistics = FeignCallMonitorAspect.getCallStatistics(serviceKey);
        
        if (statistics != null) {
            result.put("success", true);
            result.put("data", statistics);
        } else {
            result.put("success", false);
            result.put("message", "未找到服务调用统计信息");
        }
        
        result.put("timestamp", System.currentTimeMillis());
        return result;
    }
    
    /**
     * 获取服务健康状态
     * 
     * @return 健康状态信息
     */
    @Operation(summary = "获取服务健康状态")
    @GetMapping("/health-status")
    public Map<String, Object> getHealthStatus() {
        log.info("获取服务健康状态");
        
        Map<String, Object> result = new HashMap<>();
        result.put("success", true);
        result.put("data", serviceHealthMonitor.getAllHealthStatus());
        result.put("timestamp", System.currentTimeMillis());
        
        return result;
    }
    
    /**
     * 获取指定服务的健康状态
     * 
     * @param serviceName 服务名称
     * @return 健康状态信息
     */
    @Operation(summary = "获取指定服务的健康状态")
    @GetMapping("/health-status/{serviceName}")
    public Map<String, Object> getHealthStatusByService(@PathVariable String serviceName) {
        log.info("获取服务健康状态，服务: {}", serviceName);
        
        Map<String, Object> result = new HashMap<>();
        ServiceHealthMonitor.HealthStatus status = serviceHealthMonitor.getHealthStatus(serviceName);
        
        if (status != null) {
            result.put("success", true);
            result.put("data", status);
        } else {
            result.put("success", false);
            result.put("message", "未找到服务健康状态信息");
        }
        
        result.put("timestamp", System.currentTimeMillis());
        return result;
    }
    
    /**
     * 清除调用统计信息
     * 
     * @return 操作结果
     */
    @Operation(summary = "清除调用统计信息")
    @DeleteMapping("/call-statistics")
    public Map<String, Object> clearCallStatistics() {
        log.info("清除调用统计信息");
        
        FeignCallMonitorAspect.clearAllCallStatistics();
        
        Map<String, Object> result = new HashMap<>();
        result.put("success", true);
        result.put("message", "调用统计信息已清除");
        result.put("timestamp", System.currentTimeMillis());
        
        return result;
    }
    
    /**
     * 清除指定服务的调用统计信息
     * 
     * @param serviceKey 服务键名
     * @return 操作结果
     */
    @Operation(summary = "清除指定服务的调用统计信息")
    @DeleteMapping("/call-statistics/{serviceKey}")
    public Map<String, Object> clearCallStatisticsByService(@PathVariable String serviceKey) {
        log.info("清除调用统计信息，服务: {}", serviceKey);
        
        FeignCallMonitorAspect.clearCallStatistics(serviceKey);
        
        Map<String, Object> result = new HashMap<>();
        result.put("success", true);
        result.put("message", "服务调用统计信息已清除");
        result.put("timestamp", System.currentTimeMillis());
        
        return result;
    }
    
    /**
     * 手动触发健康检查
     * 
     * @return 操作结果
     */
    @Operation(summary = "手动触发健康检查")
    @PostMapping("/health-check")
    public Map<String, Object> triggerHealthCheck() {
        log.info("手动触发健康检查");
        
        serviceHealthMonitor.checkServiceHealth();
        
        Map<String, Object> result = new HashMap<>();
        result.put("success", true);
        result.put("message", "健康检查已触发");
        result.put("timestamp", System.currentTimeMillis());
        
        return result;
    }
}
